// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

// Project imports:
import 'package:palmf_hid/common/store/store.dart';
import 'package:palmf_hid/common/utils/update_helper.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'setting_state.dart';

class SettingLogic extends GetxController {
  final SettingState state = SettingState();
  var psw = "";

  @override
  void onReady() {
    super.onReady();
    packageInfo();
    _getLanguageState();
  }

  //获取选择得语言
  _getLanguageState() {
    MyLogUtil().i(ConfigStore.to.locale.languageCode.toString());
    if (ConfigStore.to.locale.languageCode.toString() == 'en') {
      state.languageChange.value = false;
    } else {
      state.languageChange.value = true;
    }
  }

  //获取版本信息
  Future<void> packageInfo() async {
    PackageInfo.fromPlatform().then((value) {
      state.appVersionName.value = value.version;
      state.appVersionNumber.value = value.buildNumber;
    });
  }

  //改变语言
  void changeLocal() {
    state.languageChange.value = !state.languageChange.value;
    if (state.languageChange.value) {
      ConfigStore.to.onLocaleUpdate(const Locale('zh', 'CN'));
    } else {
      ConfigStore.to.onLocaleUpdate(const Locale('en', 'US'));
    }
  }


  @override
  void onClose() {
    super.onClose();
    MyLogUtil().i("message");
  }

//检查更新
  void checkUpdate() {
    UpdateHelper.getSingleton()
        ?.checkUpdate(state.appVersionNumber.value, true);
  }
}
